//
//  ViewController.m
//  Json
//
//  Created by apple on 15/8/24.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *nameLable;
@property (weak, nonatomic) IBOutlet UILabel *ageLable;
@property (weak, nonatomic) IBOutlet UILabel *sexLable;
@property (weak, nonatomic) IBOutlet UILabel *bookLable;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    
    
    
    
#if 0
    
//OC-->Json
    NSDictionary *dict = @{
                           
                    @"name":@"Peter",
                    @"age":@23,
                    @"married":@(NO),
                    @"note":[NSNull null],
                    @"friends":@[@"ZhangSan",@"LiSi",@"WangWu"]
                    };
    
    //序列化
//    NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:1 error:nil];
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"str-->%@",str);
    
    [str writeToFile:@"Users/apple/Desktop/Json.json" atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
#endif
    
#if 0
    //1、将文件中内容以json字符串的形式读出；
    NSString *jsonStr = [NSString stringWithContentsOfFile:@"/Users/apple/Desktop/Json.json" encoding:NSUTF8StringEncoding error:nil ];
    
    NSLog(@"jsonStr-->%@",jsonStr);
    
    //2、将json字符串转换成二进制的形式；
    NSData *data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"data-->%@",data);
    
    NSError *error = nil;
    
    //3、将二进制的格式转换成oc格式；
    NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    
    if (!error)
    {
        NSLog(@"错了");
    }
    else
    {
        dict[@"abc"] = @"Lisi";
        NSLog(@"%@",[dict class]);
    }
    NSLog(@"dict-->%@",dict);
#endif
    
#if 0
    
//    将字典转换成自定义的对象的好处
//    1、有类型的检查；
//    2、可以封装一些数据；
    NSData *data = [NSData dataWithContentsOfFile:@"/Users/apple/Desktop/Json.json"];
    NSLog(@"%@",data);
    
    
#endif
    
#if 1
    
//    将对象转换成字典；
    Student *student = [Student new];
    student.name = @"张三";
    student.sex = @"male";
    
    NSDictionary *stuDict = [student dictionary];
    NSData *data = [NSJSONSerialization dataWithJSONObject:stuDict options:NSJSONWritingPrettyPrinted error:nil];
    NSString *stuStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"\%@",stuStr);
    
#endif
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
